package com.company.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.company.project.vo.req.goods.AlbumUpdateReqVO;
import com.company.project.vo.req.goods.AlbumInsterReqVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.company.project.mapper.SysGoodsAlbumMapper;
import com.company.project.entity.SysGoodsAlbumEntity;
import com.company.project.service.SysGoodsAlbumService;

import java.util.ArrayList;
import java.util.List;


@Service("sysGoodsAlbumService")
@Slf4j
public class SysGoodsAlbumServiceImpl extends ServiceImpl<SysGoodsAlbumMapper, SysGoodsAlbumEntity> implements SysGoodsAlbumService {


    @Override
    public List<SysGoodsAlbumEntity> insterAlbum(List<AlbumInsterReqVO> albumInsterReqVOList, String goodsId) {
        List<SysGoodsAlbumEntity> sysGoodsAlbumEntityList = new ArrayList<>();
        for (int i = 0; i < albumInsterReqVOList.size(); i ++ ){
            SysGoodsAlbumEntity sysGoodsAlbumEntity = new SysGoodsAlbumEntity();
            sysGoodsAlbumEntity.setImgUrl(albumInsterReqVOList.get(i).getImgUrl());
            sysGoodsAlbumEntity.setGoodsId(goodsId);
            sysGoodsAlbumEntity.setSort(i);
            sysGoodsAlbumEntityList.add(sysGoodsAlbumEntity);
        }
        this.saveBatch(sysGoodsAlbumEntityList);
        return sysGoodsAlbumEntityList;
    }

    @Override
    public List<SysGoodsAlbumEntity> updateAlbum(List<AlbumUpdateReqVO> albunInsterReqVOList, String goodsId) {

        List<SysGoodsAlbumEntity> updateAlbum = new ArrayList<>();
        for(int i = 0; i < albunInsterReqVOList.size(); i++ ){
            SysGoodsAlbumEntity sysGoodsAlbumEntity = new SysGoodsAlbumEntity();
            sysGoodsAlbumEntity.setId(albunInsterReqVOList.get(i).getId());
            sysGoodsAlbumEntity.setGoodsId(goodsId);
            sysGoodsAlbumEntity.setImgUrl(albunInsterReqVOList.get(i).getImgUrl());
            sysGoodsAlbumEntity.setSort(i);
            updateAlbum.add(sysGoodsAlbumEntity);
        }
        this.updateBatchById(updateAlbum);
        return updateAlbum;
    }

    @Override
    public List<String> deleteAlbum(String goodsId) {
        List<String> removeIds = new ArrayList<>();
        // 查询商品的相册
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("goods_id",goodsId);
        List<SysGoodsAlbumEntity> removeList = super.list(queryWrapper);
        for(int i = 0; i < removeList.size(); i++ ){
            removeIds.add(removeList.get(i).getId());
        }
        super.removeByIds(removeIds);
        return removeIds;
    }
}